package com.quectel.core.module.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.device.dao.DeviceDao;
import com.quectel.core.module.device.dao.DeviceTypeDao;
import com.quectel.core.module.device.dto.DeviceCategoryDto;
import com.quectel.core.module.device.dto.DeviceTypeDto;
import com.quectel.core.module.device.dto.DeviceVendorDto;
import com.quectel.core.module.device.entity.DeviceTypeEntity;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.core.module.device.service.DeviceVendorService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.redis.RedisUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.quectel.core.constants.RedisCacheConstants.PROJECT_NAME;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:11:16
 */
@DubboService
public class DeviceTypeServiceImpl implements DeviceTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceTypeServiceImpl.class);
    /**
     * 设备类型缓存
     */
    public static final String DEVICE_TYPE_CACHE = PROJECT_NAME + "DEVICE_TYPE_CACHE";

    @Autowired
    private DeviceTypeDao deviceTypeDao;
    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private DeviceCategoryService deviceCategoryService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceVendorService deviceVendorService;

    @Override
    public List<DeviceTypeDto> queryList(Map<String, Object> params) {
        List<DeviceTypeEntity> list = deviceTypeDao.queryList(params);
        List<DeviceTypeDto> dtoList = CopyUtils.copyList(list, DeviceTypeDto.class);
        if (dtoList != null) {
            //填充部分字段
            for (DeviceTypeDto deviceTypeDto : dtoList) {
                if (deviceTypeDto.getDeviceCategoryId() != null) {
                    DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectCacheById(deviceTypeDto.getDeviceCategoryId());
                    if (deviceCategoryDto != null) {
                        deviceTypeDto.setDeviceCategoryName(deviceCategoryDto.getName());
                    }
                }
                if (deviceTypeDto.getVendorId() != null) {
                    DeviceVendorDto deviceVendorDto = deviceVendorService.selectCacheById(deviceTypeDto.getVendorId());
                    if (deviceVendorDto != null) {
                        deviceTypeDto.setVendorName(deviceVendorDto.getName());
                    }
                }
            }
        }
        return dtoList;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deviceTypeDao.queryTotal(params);
    }

    @Override
    public Long save(DeviceTypeDto dto) {
        DeviceTypeEntity entity = CopyUtils.copyObj(dto, DeviceTypeEntity.class);
        entity.setId(Snowflake.nextId());
        deviceTypeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeviceTypeDto dto) {

        RedisUtils.del(DEVICE_TYPE_CACHE + dto.getId());

        DeviceTypeEntity entity = CopyUtils.copyObj(dto, DeviceTypeEntity.class);
        deviceTypeDao.updateById(entity);
    }

    @Override
    public DeviceTypeDto selectById(Long id) {

        DeviceTypeEntity entity = deviceTypeDao.selectById(id);
        if (entity == null) {
            return null;
        }
        DeviceTypeDto deviceTypeDto = CopyUtils.copyObj(entity, DeviceTypeDto.class);
        //填充部分字段
        if (deviceTypeDto.getDeviceCategoryId() != null) {
            DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectCacheById(deviceTypeDto.getDeviceCategoryId());
            if (deviceCategoryDto != null) {
                deviceTypeDto.setDeviceCategoryName(deviceCategoryDto.getName());
            }
        }
        if (deviceTypeDto.getVendorId() != null) {
            DeviceVendorDto deviceVendorDto = deviceVendorService.selectCacheById(deviceTypeDto.getVendorId());
            if (deviceVendorDto != null) {
                deviceTypeDto.setVendorName(deviceVendorDto.getName());
            }
        }
        return deviceTypeDto;
    }

    @Override
    public DeviceTypeDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEVICE_TYPE_CACHE + id,
                5 * 60
        );
    }

    @Override
    public void deleteById(Long id) {

        RedisUtils.del(DEVICE_TYPE_CACHE + id);

        deviceTypeDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public DeviceTypeDto selectByCode(String code) {
        DeviceTypeEntity deviceTypeEntity = deviceTypeDao.selectOne(new QueryWrapper<DeviceTypeEntity>().eq("code", code).last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(deviceTypeEntity, DeviceTypeDto.class);
    }

    @Override
    public List<DeviceTypeDto> selectByCategoryCode(String code) {
        List<DeviceTypeEntity> list = deviceTypeDao.selectByCategoryCode(code);
        return CopyUtils.copyList(list, DeviceTypeDto.class);
    }

    @Override
    public DeviceTypeDto selectByName(String name) {
        DeviceTypeEntity deviceTypeEntity = deviceTypeDao.selectOne(new QueryWrapper<DeviceTypeEntity>().eq("name", name).last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(deviceTypeEntity, DeviceTypeDto.class);
    }
}
